<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="分站">
              <a-input v-if="userInfo.userType == 2 || userInfo.userType == 3" disabled v-model="userInfoSite.name"></a-input>
              <a-input v-if="userInfo.userType == 1" placeholder="请输入分站名称" v-model="queryParam.siteName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="信息类型">
              <a-select @change="handleParentTypeInput" v-model="queryParam.parentType" placeholder="请选择信息类型" option-label-prop="label">
                <a-select-option v-for="(i, index) in parentTypeOptions" :key="index" :value="i.code" :label="i.title">
                  {{i.title}}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item >
              <a-select v-model="queryParam.childrenType" placeholder="请选择信息类型" option-label-prop="label">
                <a-select-option v-for="(i, index) in childrenTypeOptions" :key="index" :value="i.code" :label="i.title">
                  {{i.title}}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="信息名称">
                <a-input placeholder="请输入信息名称" v-model="queryParam.name"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="状态">
                <j-dict-select-tag placeholder="请选择状态" v-model="queryParam.status" dictCode="information_status"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchResetBefor" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" v-if="permissionData.informationAdd" type="primary" icon="plus">新增</a-button>
      <a-button @click="batchDel" v-if="permissionData.informationDelete" icon="delete">删除</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <span slot="upShelf" slot-scope="text, record">
          <span v-if="record.status === 3">
            <a-popconfirm :title="infoUpDownTagData[text]['popconfirmTitle']" @confirm="() => handleUpDown(record.id, text)">
              <!-- <a-tag class="mouse-hand" color="pink">{{infoUpDownTagData[text].text}}</a-tag> -->
              <a-tag class="mouse-hand" :color="infoUpDownTagData[text]['color']">{{infoUpDownTagData[text]['text']}}</a-tag>
            </a-popconfirm>
          </span>
          <span v-else>
            <a-tag color="geekblue">待发布</a-tag>
          </span>
        </span>

        <span slot="status" slot-scope="text, record">
          <!-- 如果状态为[审核中] 并且有审核权限 -->
          <span v-if="record.status == 1 && permissionData.informationReview">
            <a-popconfirm
              title="是否审核通过？"
              ok-text="通过"
              cancel-text="不通过"
              @confirm="() => changeReview(url.approvedReview, record.id)"
              @cancel="() => changeReview(url.notApprovedReview, record.id)">
              <a-tag class="mouse-hand" :color="statusColor[record.status]">{{text}}</a-tag>
            </a-popconfirm>
          </span>
          <!-- 如果状态为[草稿]或[已驳回] 并且是本人 -->
          <span v-else-if="(record.status == 0 || record.status == 2) && userInfo.id == record.createBy">
            <a-popconfirm
              title="是否提交审核？"
              @confirm="() => changeReview(url.submitReview, record.id)">
              <a-tag class="mouse-hand" :color="statusColor[record.status]">{{text}}</a-tag>
            </a-popconfirm>
          </span>
          <span v-else>
            <a-tag :color="statusColor[record.status]">{{text}}</a-tag>
          </span>
        </span>

        <span slot="action" slot-scope="text, record">
          <a :disabled="record.status === 1 || record.status === 3 " @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item v-if="permissionData.informationDelete">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <information-modal ref="modalForm" :threeElements="threeElements" @ok="modalFormOk" />
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { putParamsAction, getAction, upDownAction } from '@/api/manage'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import InformationModal from './modules/InformationModal'
  import { loadCategoryData } from '@/api/api'
  import { mapGetters } from 'vuex'

  export default {
    name: 'InformationList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      InformationModal
    },
    data () {
      return {
        description: '信息发布管理页面',
        statusColor:['cyan', 'orange', 'pink', 'green'],
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'信息类型',
            align:"center",
            dataIndex: 'parentType',
            customRender: (text, row, index) => {
              return `${row.parentType_dictText} - ${row.childrenType_dictText}`
            },
          },
          {
            title:'信息名称',
            align:"center",
            dataIndex: 'name'
          },
          {
            title:'分站',
            align:"center",
            dataIndex: 'siteId_dictText'
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'status_dictText',
            scopedSlots: { customRender: 'status' },
          },{
            title:'上下架',
            align:"center",
            dataIndex: 'upShelf',
            scopedSlots: { customRender: 'upShelf' },
          },
          {
            title:'编号',
            align:"center",
            dataIndex: 'code'
          },
          {
            title:'阅读数量',
            align:"center",
            sorter: true,
            dataIndex: 'readQty'
          },
          {
            title:'回复数量',
            align:"center",
            sorter: true,
            dataIndex: 'replyQty'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/information/list",
          delete: "/information/delete",
          deleteBatch: "/information/deleteBatch",
          submitReview: "/information/submitReview",
          approvedReview: "/information/approvedReview",
          notApprovedReview: "/information/notApprovedReview",
          putOnSale: "/information/putOnSale",
          pullOffShelves: "/information/pullOffShelves",
        },
        infoUpDownTagData: {
          '0':{
            color:"green",
            text: "已上架",
            popconfirmTitle:"确定下架吗?"
          },
          '1':{
            color:"pink",
            text: "已下架",
            popconfirmTitle:"确定上架吗?"
          }
        },
        dictOptions:{},
        parentTypeOptions: [],
        childrenTypeOptions: [],
        threeElements: null
      }
    },
    created() {
      this.loadParentType();
      this.threeElements = {
        site: this.userInfoSite ? this.userInfoSite['name'] : null
      }
    },
    computed: {
      ...mapGetters(["permissionData", "userInfo", "userInfoSite"]),
    },
    methods: {
      changeReview(url, id) {
        putParamsAction(url, {id }).then(res=>{
          console.log(res)
          if (res.success) {
            //重新计算分页问题
            this.reCalculatePage(1)
            this.$message.success(res.message);
            this.loadData();
          } else {
            this.$message.warning(res.message);
          }
        })
      },
      searchResetBefor() {
        this.childrenTypeOptions = []
        this.searchReset()
      },
      handleParentTypeInput(e='') {
        delete this.queryParam['childrenType']
        // this.queryParam.childrenType = null
        getAction("/sys/category/loadTreeData",{pcode: e}).then(res=>{
          this.childrenTypeOptions = res.result
        })
      },
      loadParentType() {
        getAction("/sys/category/loadTreeData", {pcode: 'B03'}).then(res=>{
          this.parentTypeOptions = res.result
        })
      },
      initDictConfig(){
        loadCategoryData({code:'B03'}).then((res) => {
          if (res.success) {
            this.$set(this.dictOptions, 'parentType', res.result)
          }
        })
      },
      handleUpDown(id, status){
        switch (status) {
          case 1: 
            return this.putOnSale(id)
          case 0:
            return this.pullOffShelves(id)
          default:
            return
        }
      },
      putOnSale(id) {
        let that = this;
        that.loading = true;
        upDownAction(that.url.putOnSale, {id: id}).then((res) => {
          if (res.success) {
            //重新计算分页问题
            that.reCalculatePage(that.selectedRowKeys.length)
            that.$message.success(res.message);
            that.loadData();
            that.onClearSelected();
          } else {
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.loading = false;
        });
      },
      pullOffShelves(id) {
        let that = this;
        that.loading = true;
        upDownAction(that.url.pullOffShelves, {id: id}).then((res) => {
          if (res.success) {
            //重新计算分页问题
            that.reCalculatePage(that.selectedRowKeys.length)
            that.$message.success(res.message);
            that.loadData();
            that.onClearSelected();
          } else {
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.loading = false;
        });
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>